package com.sinosoft.validateCode.image;

import com.sinosoft.validateCode.util.CutRange;
import com.sinosoft.validateCode.util.DrawingUtil;
import com.sinosoft.validateCode.util.ImageUtil;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * 图片验证码方法
 * @author handong
 */
public class ImageValidateCodeUtil {


    /**
     * 背景图宽度
     */
    private int width = 400;
    /**
     * 背景图高度
     */
    private int height = 250;

    /**
     * 抠图模型地址
     */
    private String templatePath ;

    /**
     * 背景图资源池
     */
    private String[] filePathList ;


    /**
     * 图片移动偏移量
     */
    private int drift_rate = 3;


    /**
     * 创建校验组合 第一步
     * @return
     */
    public ImageValidateCombo createCombo(HttpServletRequest request) throws IOException {

        ImageValidateCombo imageValidateCombo = new ImageValidateCombo();

        //创建校验号
        String validateNo = UUID.randomUUID().toString();
        //随机选择资源
        int imageIndex = (int) (Math.random()*filePathList.length);
        //创建原始图片
        BufferedImage template = ImageIO.read(new File(templatePath));
        BufferedImage source = ImageUtil.randomCutImage(new File(filePathList[imageIndex]),width,height);


        //生成背景图片 （随机位置加蒙版)
        Point pointMove =  randomMoveImage(width,height,template);
        //生成蒙版图片
        BufferedImage moveImage =     DrawingUtil.createBackground(template,source,pointMove.getX(),pointMove.getY());
        //创建可移动图片
        BufferedImage backgroundImage =    DrawingUtil.createMoveImage(template,source,pointMove.getX(),pointMove.getY());

        Point moveImagePoint = new Point(0, pointMove.getY());
        //组装对象
        imageValidateCombo.setValidateNo(validateNo);
        imageValidateCombo.setImagePoint(moveImagePoint);
        imageValidateCombo.setBackgroundImage(backgroundImage);
        imageValidateCombo.setImage(moveImage);

        //设置题目答案
        Answer answer = new Answer();
        answer.setMoveSize(pointMove.getX());


        System.out.printf("截取图片位置：x="+pointMove.getX() +": y="+pointMove.getY());


        //指定本次使用的校验码
        //request.setAttribute("validateNo",validateNo);
        //request.setAttribute(validateNo+"_answer",answer);

        return imageValidateCombo;
    }

    /***
     * 随机生成截图坐标
     * @return
     */
    public Point randomMoveImage(int width , int height , BufferedImage template){
        int x = (int) (Math.random()*(width/2-template.getWidth())) + width/2;
        int y = (int)  (Math.random()*(height-template.getHeight()));
        return new Point(x,y);
    }



    /**
     * 第一次校验  第二步
     * @param validateNo
     * @param moveSize
     * @return
     */
    public ValidateResult validate_1(String validateNo , int moveSize, HttpServletRequest request){
        ValidateResult result = new ValidateResult();
        //正确答案
        Answer right_answer = (Answer) request.getSession().getAttribute(validateNo+"_answer");
        if (right_answer.answer(moveSize ,null , drift_rate)){
            String  validate_1_code = UUID.randomUUID().toString();
            request.getSession().setAttribute(validateNo+"validate_1_code" , validate_1_code);
            result.setResult(true);
            result.setValidateCode(validate_1_code);
        }else{
            //校验失败
            result.setResult(false);
        }

        return  result;
    }


    /**
     * 第二次校验 第三步
     * @param validateCode
     * @return
     */
    public boolean validate_2(String validateNo , String validateCode, HttpServletRequest request){

        String right_validate_1_code = (String) request.getSession().getAttribute(validateNo+"validate_1_code" );

        return right_validate_1_code.equals(validateCode) ;
    }







    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public String[] getFilePathList() {
        return filePathList;
    }

    public void setFilePathList(String[] filePathList) {
        this.filePathList = filePathList;
    }

    public String getTemplatePath() {
        return templatePath;
    }

    public void setTemplatePath(String templatePath) {
        this.templatePath = templatePath;
    }


    public int getDrift_rate() {
        return drift_rate;
    }

    public void setDrift_rate(int drift_rate) {
        this.drift_rate = drift_rate;
    }

    public ImageValidateCodeUtil(int width, int height, String templatePath, String[] filePathList) {
        this.width = width;
        this.height = height;
        this.templatePath = templatePath;
        this.filePathList = filePathList;
    }

    public ImageValidateCodeUtil(int width, int height, String templatePath, String[] filePathList, int drift_rate) {
        this.width = width;
        this.height = height;
        this.templatePath = templatePath;
        this.filePathList = filePathList;
        this.drift_rate = drift_rate;
    }
}
